package net.zjitc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/11/13.
 */
@WebFilter(filterName = "LoginFilter",urlPatterns = "/loginServlet",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpSession session = req.getSession();
        Object admin = session.getAttribute("admin");
        if(admin==null)
        {
            req.setAttribute("msg","你还没登陆，请先登陆");
            req.getRequestDispatcher("/admin/login.jsp").forward(servletRequest,servletResponse);
        }else {
            chain.doFilter(servletRequest,servletResponse);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
